﻿/*
 * BFind - A tool for finding football matches for trading on Betfair
 * Copyright 2009, 2010 Magnus Olsson <magnus72(at)gmail.com>
 * 
 * This file is part of BFind.
 * 
 * BFind is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BFind is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BFind.  If not, see <http://www.gnu.org/licenses/>
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace betfairfind.Rules
{
    class AllGames: BaseRule
    {
        public AllGames(Dictionary<string, string> config, List<MyMarket> inplaymarkets, Dictionary<long, MarketPricesCompressed> marketprices)
            : base()
        {
            MarketsUsedInside = new string[] { "match odds"};

            Logger.Log("===============================================================================", true, true);
            Logger.Log("All matches (Date/Time, matchup, home, away, draw, total matched)", true, true);
            Logger.Log("-------------------------------------------------------------------------------", true, true);

            if (!CheckRequiredMarkets()) { return; } 

            string[] games = FindSoccerGames.GetMatches();

            string delim = config["Rule.AllGames.DELIMITER"];

            foreach (var game in games)
            {
                try
                {
                    MyMarket market_mo = FindSoccerGames.GetMarket(game, "match odds");
                    MarketPricesCompressed mpc_mo = marketprices[market_mo.marketId];
                    double homeodds = mpc_mo.GetBestBackOdds(0);
                    double awayodds = mpc_mo.GetBestBackOdds(1);
                    double drawodds = mpc_mo.GetBestBackOdds(2);
                    double matched = mpc_mo.TotalMatched;

                    Logger.Log(market_mo.marketTime + delim + BF.convertPathToGame(game) + delim + homeodds + delim + awayodds + delim + drawodds + delim + config["CURRENCY_SIGN"] + matched, true, true);
                }
                catch (Exception  ex)
                {
                    Logger.LogError("Error in game " + game + Environment.NewLine + ex.ToString(), true, false);
                }
            }
            if (games.Count() == 0)
            {
                Logger.Log("No matches returned within the requested time period", true, true);
            }
        }
    }
}
